package com.firestone.service.center.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.firestone.bean.dto.manager.ManagerDTO;
import com.firestone.bean.entity.mysql.primary.ImCenterManager;
import com.firestone.bean.entity.mysql.primary.ImCenterManagerRRole;
import com.firestone.dao.primary.center.ImCenterManagerDao;
import com.firestone.service.center.IImCenterManagerRRoleService;
import com.firestone.service.center.IImCenterManagerService;

/**
 * <p>
 * 产业地图后端管理中心管理员表 服务实现类
 * </p>
 *
 * @author CAIYJ
 * @since 2018-03-29
 */
@Service
public class ImCenterManagerServiceImpl
        extends ServiceImpl<ImCenterManagerDao, ImCenterManager>
        implements IImCenterManagerService {

    @Autowired
    private IImCenterManagerRRoleService imCenterManagerRRoleService;

    @Override
    public Page<ManagerDTO> listManager(Page<ManagerDTO> page, String name)
            throws Exception {
        EntityWrapper<ManagerDTO> ew = new EntityWrapper<>();
        ew.setEntity(new ManagerDTO());
        ew.where(StringUtils.isNotBlank(name),
                "cm.`name` LIKE concat('%',{0},'%')", name);

        ew.orderBy("cm.update_time", false);
        List<ManagerDTO> list = this.baseMapper.selectManagerList(page, ew);
        return page.setRecords(list);
    }

    @Override
    @Transactional
    public boolean saveManager(ImCenterManager manager, Long roleId)
            throws Exception {
        boolean result = this.insert(manager);
        if (roleId != null) {
            ImCenterManagerRRole imCenterManagerRRole = new ImCenterManagerRRole();
            imCenterManagerRRole.setManagerId(manager.getId());
            imCenterManagerRRole.setRoleId(roleId);
            result = imCenterManagerRRoleService.insert(imCenterManagerRRole);
        }
        return result;
    }

    @Override
    @Transactional
    public boolean updateManager(ImCenterManager manager, Long roleId)
            throws Exception {
        boolean result = false;
        if (manager != null && manager.getId() != null) {
            result = this.updateById(manager);
            EntityWrapper<ImCenterManagerRRole> ew = new EntityWrapper<>();
            ew.setEntity(new ImCenterManagerRRole());
            ew.where("manager_id = '{0}'", manager.getId());
            if (roleId == null) {
                result = imCenterManagerRRoleService.delete(ew);
            } else {
                ImCenterManagerRRole imCenterManagerRRole = new ImCenterManagerRRole();
                imCenterManagerRRole.setRoleId(roleId);
                result = imCenterManagerRRoleService
                        .update(imCenterManagerRRole, ew);
            }
        }
        return result;
    }

    @Override
    public boolean removeManager(ImCenterManager manager) throws Exception {
        boolean result = false;
        if (manager != null && manager.getId() != null) {
            EntityWrapper<ImCenterManagerRRole> ew = new EntityWrapper<>();
            ew.setEntity(new ImCenterManagerRRole());
            ew.where("manager_id = '{0}'", manager.getId());
            result = imCenterManagerRRoleService.delete(ew);
            result = this.updateById(manager);
        }
        return result;
    }

}
